<?php
/*
 * 后台系统权限分配处理
 * @arrange Mark
 * @package ixdcw
 * @file    index.inc.php
 * @time    12-12-19 上午10:22
 */
defined('IN_IXDCW') or exit('Access Denied');
$menus = array(
	array('系统首页', '?action=main'),
	array('统计信息', '?action=count'),
	array('清空缓存', '?action=clear_cache'),
	array('修改密码', '?action=password'),
	array('个人资料中心', $MODULE[2]['linkurl'], 'target="_blank"'),
	array('网站首页', SITE_URL, 'target="_blank"'),
	array('安全退出', '?file=logout', 'target="_top" onclick="return confirm(\'确定要退出管理吗?\');"'),
);
if ($_admin > 1) unset($menus[1]);
switch ($action) {
	/*清空缓存  */
	case 'clear_cache':
		isset($step) or $step = 0;
		if ($step == 0) {
			PCache::clear('keylink');
			PCache::sysKeylink();
			PCache::sysJs();
			PCache::clear('option');
			msg('正在开始清空缓存', '?action=' . $action . '&step=' . ($step + 1));
		} else if ($step == 1) {
			PCache::sysClearTag(true);
			msg('标签调用缓存清空成功', '?action=' . $action . '&step=' . ($step + 1));
		}else if ($step == 2) {
			PCache::clear('html', 'dir', 'index_cache');
			msg('首页缓存清空成功', '?action=' . $action . '&step=' . ($step + 1));
		}else if ($step == 3) {
			PCache::clear('php', 'dir', 'xdTpl');
			msg('模板缓存清空成功', '?action=' . $action . '&step=' . ($step + 1));
		}else if ($step == 4) {
			$a = new AreaModule();
			$areas = $a->getChildrenJs(0);
			File::put(DT_ROOT.'/Public/data/area.json', json_encode($areas));
			msg('地区缓存清空成功', '?action=' . $action . '&step=' . ($step + 1));
		} else if ($step == 5) {
			PCache::clearAll();
			msg('全部缓存清空成功');
		}
		break;
	case 'cacheclear':
		if ($CFG['cache'] == 'file') dheader('?action=update');
		$dc->clear();
		msg('缓存更新成功');
		break;
	case 'update':
		$job = 'php';
		if (isset($dir)) {
			isset($cf) or $cf = 0;
			isset($cd) or $cd = 0;
			if (preg_match("/^" . $job . "[0-9]{14}$/", $dir)) {
				$dirs = glob(DT_CACHE . '/' . $dir . '/*');
				if ($dirs) {
					$sub = $dirs[array_rand($dirs)];
					File::del($sub . '/index.html');
					$files = glob($sub . '/*.php');
					if ($files) {
						$i = 0;
						foreach ($files as $f) {
							File::del($f);
							$cf++;
							$i++;
							if ($i > 500) msg('已删除 ' . $cd . ' 个目录，' . $cf . ' 个文件' . progress(0, $cd, $tt), '?action=' . $action . '&dir=' . $dir . '&cd=' . $cd . '&cf=' . $cf . '&job=' . $job . '&tt=' . $tt, 0);
						}
						File::dirDelete($sub);
						$cd++;
						msg('已删除 ' . $cd . ' 个目录，' . $cf . ' 个文件' . progress(0, $cd, $tt), '?action=' . $action . '&dir=' . $dir . '&cd=' . $cd . '&cf=' . $cf . '&job=' . $job . '&tt=' . $tt, 0);
					} else {
						File::dirDelete($sub);
						$cd++;
						msg('已删除 ' . $cd . ' 个目录，' . $cf . ' 个文件' . progress(0, $cd, $tt), '?action=' . $action . '&dir=' . $dir . '&cd=' . $cd . '&cf=' . $cf . '&job=' . $job . '&tt=' . $tt, 0);
					}
				} else {
					File::dirDelete(DT_CACHE . '/' . $dir);
					msg('缓存更新成功');
				}
			} else {
				msg('目录名错误');
			}
		} else {
			$dir = $job . timetodate($DT_TIME, 'YmdHis');
			if (rename(DT_CACHE . '/' . $job, DT_CACHE . '/' . $dir)) {
				File::dirCreate(DT_CACHE . '/' . $job);
				File::del(DT_CACHE . '/' . $dir . '/index.html');
				$dirs = glob(DT_CACHE . '/' . $dir . '/*');
				$tt   = count($dirs);
				msg('正在更新，此操作可能用时较长，请不要中断..', '?action=' . $action . '&dir=' . $dir . '&job=' . $job . '&tt=' . $tt);
			} else {
				msg('更新失败');
			}
		}
		break;
	/*更新扩展  */
	case 'html':
		PCache::sysClearTag(true);
		$db->expires = $CFG['db_expires'] = $CFG['tag_expires'] = 0;
		tohtml('index');
		$filename = $CFG['com_dir'] ? DT_ROOT . '/' . $DT['index'] . '.' . $DT['file_ext'] : DT_CACHE . '/index.inc.html';
		msg('首页更新成功 ' . (is_file($filename) ? dround(filesize($filename) / 1024) . 'Kb ' : '') . '&nbsp;&nbsp;<a href="' . SITE_URL . '" target="_blank">点击查看</a>');
		break;
	case 'phpinfo':
		phpinfo();
		exit;
		break;
	case 'password':
		if ($submit) {
			if (!$oldpassword) msg('请输入现有密码');
			if (!$password) msg('请输入新密码');
			if (strlen($password) < 6) msg('新密码最少6位，请修改');
			if ($password != $cpassword) msg('两次输入的密码不一致，请检查');
			$r = $db->getOne("SELECT password FROM {$db->getPre()}member WHERE userid='$_userid'");
			if ($r['password'] != md5(md5($oldpassword))) msg('现有密码错误，请检查');
			if ($password == $oldpassword) msg('新密码不能与现有密码相同');
			$password = md5(md5($password));
			$db->query("UPDATE {$db->getPre()}member SET password='$password' WHERE userid='$_userid'");
			msg('管理员密码修改成功', '?action=main');
		} else {
			include tpl('password');
		}
		break;
	case 'side':
		$files = glob(DT_CACHE . '/*.part');
		if ($files) {
			foreach ($files as $f) {
				$mid  = basename($f, '.part');
				$fd   = $mid == 4 ? 'userid' : 'itemid';
				$r    = $db->getOne("SELECT MAX($fd) AS maxid FROM " . get_table($mid));
				$part = split_id($r['maxid']);
				split_content($mid, $part);
				split_content($mid, $part + 1);
			}
		}
		/*
		if(isset($MODULE[5])) {
			$r = $db->getOne("SELECT MAX(itemid) AS maxid FROM {$db->getPre()}sell");
			$part = split_id($r['maxid']);
			split_sell($part);
			split_sell($part+1);
		}
		*/
		$dc->expire();
		include tpl('side');
		break;
	case 'main':
		if ($submit) {
			$note = '<?php exit;?>' . stripslashes($note);
			File::put(DT_ROOT . '/file/user/' . dalloc($_userid) . '/' . $_userid . '/note.php', $note);
			dmsg('更新成功', '?action=main');
		} else {
			$user = $db->getOne("SELECT loginip,logintime,logintimes FROM {$db->getPre()}member WHERE userid=$_userid");
			$note = DT_ROOT . '/file/user/' . dalloc($_userid) . '/' . $_userid . '/note.php';
			$note = File::get($note);
			if ($note) {
				$note = substr($note, 13);
			} else {
				$note = '';
			}
			$install = File::get(DT_CACHE . '/install.lock');
			if (!$install) {
				$install = $DT_TIME;
				File::put(DT_CACHE . '/install.lock', $DT_TIME);
			}
			$r          = $db->getOne("SELECT item_value FROM {$db->getPre()}setting WHERE item='destoon' AND item_key='backtime'");
			$backtime   = $r['item_value'];
			$backdays   = intval(($DT_TIME - $backtime) / 86400);
			$backtime   = timetodate($backtime, 6);
			$notice_url = Crypt::decode('B2BVIgIhAicINwUtD3MFcgUjV3dccFM9C2IFJ1EiVzQCYlY7AHkBNwBqAWtRLQFiAD0BP1QzAmYOdlQrBXJRNQd4', 'destoon') . '?action=notice&product=b2b&lang=' . DT_LANG . '&charset=' . DT_CHARSET . '&domain=' . DT_DOMAIN . '&install=' . $install . '&os=' . PHP_OS . '&soft=' . urlencode($_SERVER['SERVER_SOFTWARE']) . '&php=' . urlencode(phpversion()) . '&mysql=' . urlencode(mysql_get_server_info()) . '&url=' . urlencode($DT_URL) . '&site=' . urlencode($DT['sitename']) . '&auth=' . strtoupper(md5($DT_URL . $install . $_SERVER['SERVER_SOFTWARE']));
			$install    = timetodate($install, 5);
			include tpl('main');
		}
		break;
	case 'count':
		$year  = isset($year) ? intval($year) : date('Y');
		$month = isset($month) ? intval($month) : 0;
		$mid   = isset($mid) ? intval($mid) : 0;
		include tpl('count');
		break;
	case 'counter':
		$today = strtotime(timetodate($DT_TIME, 3) . ' 00:00:00');
		//

		$num = $db->count($db->getPre() . 'finance_charge', "status=0", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("charge").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'finance_cash', "status=0", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("cash").innerHTML="' . $num . '";}catch(e){}';

		//待审核贸易提醒

		$num = $db->count($db->getPre() . 'alert', "status=2", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("alert").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'guestbook', "edittime=0", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("guestbook").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'link', "status=2 AND username=''", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("link").innerHTML="' . $num . '";}catch(e){}';


		$num = $db->count($db->getPre() . 'link', "status=2 AND username<>''", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("comlink").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'keyword', "status=2", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("keyword").innerHTML="' . $num . '";}catch(e){}';


		//待审核实名认证
		$num = $db->count($db->getPre() . 'validate', "status=2", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("validate").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'ad', "status=2", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("ad").innerHTML="' . $num . '";}catch(e){}';

		//会员
		$num = $db->count($db->getPre() . 'member');
		echo 'try{document.getElementById("member").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'upgrade', "status=2");
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("member_vip").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'member', "groupid=4", 60);
		$num = $num ? '<strong class=\"f_red\">' . $num . '</strong>' : 0;
		echo 'try{document.getElementById("member_check").innerHTML="' . $num . '";}catch(e){}';

		$num = $db->count($db->getPre() . 'member', "regtime>$today", 60);
		echo 'try{document.getElementById("member_new").innerHTML="' . $num . '";}catch(e){}';
		break;
	case 'left':
		$mymenu = PCache::read('menu-' . $_userid . '.php');
		include tpl('left');
		break;
	default:
		include tpl('index');
		break;
}
?>